package eHealth.android.REST;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONObject;

import android.widget.Toast;

/**
 * 
 * @author Thomas Rouvinez
 * Date 12.02.2012
 * 
 * Goal : this object takes away the code for the REST calls in the application
 * from the GUI. We have the method to login, send a record and get the statistics
 * of a user.
 *
 */
public abstract class RESTMethods {

	/**
	 * Request Login : success is based on the server response code (200 => OK).<br>
	 * Only a user which is a patient is granted access.
	 * @param userName
	 * @param password
	 * @return a boolean whether the login was successful or not.
	 * @throws Exception
	 */
	public static boolean login(String userName, String password) throws Exception{
		boolean loginSuccess = false;
		int response = 0;

		// Prepare REST client with URI to the server's method.
		RestClient client = new RestClient(eHealth.android.client.Menu.RESTURILogin);

		// Transform login into JSON object.
		JSONObject login = new JSONObject();

		login.put("login", userName);	
		login.put("password", password);

		// Send JSON object to the REST server.
		try {
			response = client.sendLogin(login, "login");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Get the server's response to know if the login worked.
		if(response == 1){
			// Grant access to the application.
			loginSuccess = true;
		}
		else{
			//Throw the exception away with the HTTP code indication.
			Toast.makeText(eHealth.android.client.Logon.context, "Login failed, try again", 500).show();
			throw new HTTPException("HTTP exception : " + client.getResponseCode());
		}

		// Return login success status.
		return loginSuccess;
	}
	
	/**
	 * Destroy a session on the server.
	 * @return nothing at all
	 * @throws Exception
	 */
	public static void destroySession() throws Exception{

		// Prepare REST client with URI to the server's method.
		RestClient client = new RestClient(eHealth.android.client.Menu.RESTURILogin);

		// Send JSON object to the REST server.
		try {
			client.destroySession();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Send a record to the server.
	 * @param array
	 * @return a boolean whether sending the record succeeded.
	 * @throws HTTPException
	 */
	public static boolean sendRecord(JSONArray array) throws HTTPException{
		// Prepare REST client with URI to the server's method.
		RestClient client = new RestClient(eHealth.android.client.Menu.RESTURIAddRecord);

		// Send JSON object to the REST server.
		try {
			client.sendRecord(array);
			Toast.makeText(eHealth.android.client.Menu.context, "Record sent successfully", 500).show();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return true;
	}

	/**
	 * Get statistics by user id.
	 * @param userID
	 * @return  null
	 */
	@SuppressWarnings("rawtypes")
	public static ArrayList getStatistics(int userID){
		return null;
	}
}

/**
 * 
 * @author Thomas Rouvinez
 * Date 12.02.2012
 * 
 * Goal: own exception class for HTTP codes else than 200 (OK).
 *
 */
@SuppressWarnings("serial")
class HTTPException extends Exception{
	public HTTPException(String msg){
		super(msg);
	}
}



